﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinSCP;
using System.IO;

//http://winscp.net/eng/docs/library

namespace Backup_IOS_Documents
{
    class Program
    {
        static void Main(string[] args)
        {
            string SaveToPath = @"D:\Jailbreak\EasyCopy\IPhone4S";
            string HostName = "127.0.0.1";
            string UserName = "root";
            string Password = "alpine";

            try
            {
                // Setup session options
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Sftp,
                    HostName = HostName,
                    UserName = UserName,
                    Password = Password,
                    GiveUpSecurityAndAcceptAnySshHostKey = true
                };

                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);

                    //Backup SMS
                    Console.WriteLine("Backing up - SMS");
                    if (!Directory.Exists(Path.Combine(SaveToPath, "ios-SMS")))
                        Directory.CreateDirectory(Path.Combine(SaveToPath, "ios-SMS"));
                    session.GetFiles("/private/var/mobile/Library/SMS/", Path.Combine(SaveToPath, "ios-SMS"), true);

                    //Backup AddressBook
                    Console.WriteLine("Backing up - AddressBook");
                    if (!Directory.Exists(Path.Combine(SaveToPath, "ios-AddressBook")))
                        Directory.CreateDirectory(Path.Combine(SaveToPath, "ios-AddressBook"));
                    session.GetFiles("/private/var/mobile/Library/AddressBook/", Path.Combine(SaveToPath, "ios-AddressBook"), true);

                    //Backup Calendar
                    Console.WriteLine("Backing up - Calendar");
                    if (!Directory.Exists(Path.Combine(SaveToPath, "ios-Calendar")))
                        Directory.CreateDirectory(Path.Combine(SaveToPath, "ios-Calendar"));
                    session.GetFiles("/private/var/mobile/Library/Calendar/", Path.Combine(SaveToPath, "ios-Calendar"), true);

                    //Backup Application Documents
                    Console.WriteLine("Backing up - Application Settings");
                    string Path_Applications = "/private/var/mobile/Applications/";
                    RemoteDirectoryInfo directory = session.ListDirectory(Path_Applications);

                    var sb = new StringBuilder();

                    foreach (RemoteFileInfo fileInfo in directory.Files)
                    {
                        if (fileInfo.IsDirectory)
                        {
                            var AppName = fileInfo.Name;

                            if (AppName == "." || AppName == "..")
                            {
                            }
                            else
                            {
                                //this is an application
                                //iterate thru all the files to find one ending with .app (this is the app name)
                                var hasDocuments = false;
                                foreach (RemoteFileInfo fi in session.ListDirectory(Path_Applications + fileInfo.Name + "/").Files)
                                {
                                    if (fi.Name.EndsWith(".app"))
                                    {
                                        //this is the appName
                                        AppName = fi.Name.Replace(".app", "") + " " + AppName;
                                    }
                                    else if (fi.Name == "Documents")
                                    {
                                        hasDocuments = true;
                                    }
                                }
                                sb.Append(AppName);
                                sb.Append(Environment.NewLine);

                                if (hasDocuments && false)
                                {
                                    Console.WriteLine("Application: " + AppName);

                                    if (!Directory.Exists(Path.Combine(SaveToPath, AppName)))
                                        Directory.CreateDirectory(Path.Combine(SaveToPath, AppName));

                                    try
                                    {
                                        session.GetFiles(Path_Applications + fileInfo.Name + "/Documents/", Path.Combine(SaveToPath, AppName), true);
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(ex);
                                    }
                                }
                            }
                        }
                    }

                    File.WriteAllText(Path.Combine(SaveToPath, "Applications List.txt"), sb.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
            }

            Console.WriteLine("------------- Completed ---------");
            Console.ReadLine();
        }
    }
}
